package com.wen.cim.common.util;

import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

/**
 * @author 文洪武
 * @title: NettyAttrUtil
 * @projectName cim-master
 * @description:
 *
 * 对channel时间获取与修改工具类
 * @date 2021/11/2216:09
 */
public class NettyAttrUtil {

    // channel中map中插入readTimer
    //Channel上的AttributeMap就是大家共享的，AttributeMap这是是绑定在Channel或者ChannelHandlerContext上的一个附件
    private static final AttributeKey<String> ATTR_KEY_READER_TIME = AttributeKey.valueOf("readerTime");

    public static void updateReaderTime(Channel channel, Long time) {
        channel.attr(ATTR_KEY_READER_TIME).set(time.toString());
    }

    public static Long getReaderTime(Channel channel) {
        String value = getAttribute(channel, ATTR_KEY_READER_TIME);

        if (value != null) {
            return Long.valueOf(value);
        }

        return null;
    }

    public static String getAttribute(Channel channel, AttributeKey<String> key) {
        Attribute<String> attr = channel.attr(key);
        return attr.get();
    }

}
